package com.template.controller.websocket;

import com.template.controller.websocket.dto.Message;
import com.template.controller.websocket.dto.MessageR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;

@Slf4j
@Controller
public class MessageController {

    @MessageMapping("/message")
    @SendTo("/topic/to")
    public MessageR toAll(Message message) {
        log.warn("接收到消息 {}", message.getMessageContent());

        return new MessageR(message.getMessageContent());
    }

    @MessageMapping("/private-message")
    @SendToUser(value = "/topic/to-user", broadcast = false)
    public MessageR toSelf(Message message) {
        log.warn("接收到消息 {}", message.getMessageContent());
        return new MessageR(message.getMessageContent());
    }

    /**
     * 当有客户端订阅该内容，会有一次性响应
     */
    @SubscribeMapping("/message")
    public String subGetMessage() {
        return "thank you subscribe my channel";
    }
}
